package com.shimepu.fragmentdemo.register;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import util.OperateData;
import com.shimepu.fragmentdemo.R;
import com.shimepu.fragmentdemo.login.LoginActivity;
import java.net.MalformedURLException;
import java.net.URL;

public class RegisterActivity extends Activity implements View.OnClickListener{
    private Button register;
    private Button getcodebutton;
    private EditText phone;
    private EditText mima;
    private EditText yanzhengma;
    private CheckBox tongyi;
    private String[] str = null;
    private static final String URLREGISTER = "xxx/register/json/data";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.register);

        //控件绑定
        init();

        //注册按钮
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputphone = phone.getText().toString();
                String inputmima = mima.getText().toString();
                String inputyanzhenma = yanzhengma.getText().toString();



                if (TextUtils.isEmpty(inputphone)) {
                    //手机号为空
                    Toast.makeText(RegisterActivity.this, "请输入11位手机号", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(inputmima)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(inputyanzhenma)) {
                    Toast.makeText(RegisterActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                } else {
                    str = new String[]{inputphone, inputmima, inputyanzhenma};

                    Handler handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            switch (msg.what) {
                                case 0:
                                    Toast.makeText(RegisterActivity.this, "服务器连接失败", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1:
                                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    //注册成功跳转到登录页面
                                    startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                                    RegisterActivity.this.finish();
                                    break;
                                case 2:
                                    Toast.makeText(RegisterActivity.this, "用户已存在", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3:
                                    Log.e("input error", "url为空");
                                    break;
                                case 4:
                                    Toast.makeText(RegisterActivity.this, "连接超时", Toast.LENGTH_SHORT).show();
                                    break;
                                default:
                            }
                        }
                    };
                    OperateData operateData = new OperateData();
                    String jsonString = operateData.stringTojson2(str);
                    URL url = null;
                    try {
                        url = new URL(URLREGISTER);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                    operateData.sendData(jsonString, handler, url);
                }


            }
        });
    }

    /**
     * 绑定控件
     */
    private void init() {

        register = findViewById(R.id.register);
        phone = findViewById(R.id.phone);
        mima = findViewById(R.id.mima);
        yanzhengma = findViewById(R.id.yanzhengma);
        getcodebutton=findViewById(R.id.getcodebutton);
        tongyi=findViewById(R.id.tongyi);
        //绑定单击监听事件
        tongyi.setOnClickListener(listener);
        //将协议checkbox默认设置为未选择状态
        tongyi.setChecked(false);
        //将注册按钮默认设置为未启用状态
        register.setEnabled(false);
    }

    //监听事件：判断同意按钮有没有被点击
   private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //如果协议被选中
            if(tongyi.isChecked()){
                //将注册按钮设置为启用状态
                register.setEnabled(true);

            }
            else
            {
                //将注册按钮设置为未启用状态
                register.setEnabled(false);
            }
        }
    };
    @Override
    public void onClick(View v) {

    }

}
